package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanServer;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author: 31043
 * @Description: 护理计划业务层
 * @Date: 2024/1/5 AM 10:17
 * @Version: 1.0
 */
@Service
public class NursingPlanServerImpl implements NursingPlanServer {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 查询所有启动的计划
     */
    @Override
    public List<NursingPlanVo> queryAllStatus() {
        List<NursingPlanVo> nursingPlans = nursingPlanMapper.queryAllStatus();
        return nursingPlans;
    }

    /**
     * 分页条件查询
     *
     * @param name 护理计划名称
     * @param status 护理计划状态
     * @param pageNum 页码（默认为1）
     * @param pageSize 每页大小（默认为10）
     */
    @Override
    public PageResponse<NursingPlanVo> queryAllPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<NursingPlanVo> nursingPlanVos =  nursingPlanMapper.queryNursingPlans(name,status);
        Page<NursingPlanVo> page = (Page<NursingPlanVo>) nursingPlanVos;
        return PageResponse.of(page, NursingPlanVo.class);
    }

    /**
     * 根据id查询护理计划
     * @param id 护理计划id
     */
    @Override
    public NursingPlanVo queryById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.getNursingPlanById(id);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        List<NursingProjectPlanVo> nursingProjectPlans = nursingProjectPlanMapper.queryByPlanId(id);
        nursingPlanVo.setProjectPlans(nursingProjectPlans);

        System.out.println(nursingPlanVo);


        return nursingPlanVo;
    }

    /**
     * 新增护理计划
     * @param nursingPlanDto 新增的计划数据
     */
    @Override
    @Transactional
    public void insert(NursingPlanDto nursingPlanDto) {
       nursingPlanDto.setStatus(0);
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.insert(nursingPlan);
        Long id = nursingPlan.getId();
        System.out.println("新增的护理计划 id：" + id);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(!CollectionUtils.isEmpty(projectPlans)){
            projectPlans.forEach(item -> {
                item.setPlanId(id);
            });
            nursingProjectPlanMapper.insertBatch(projectPlans);
        }
    }


    /**
     * 更新护理计划
     * @param nursingPlanDto 新增的计划数据
     */
    @Override
    @Transactional
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.update(nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
        if(!CollectionUtils.isEmpty(projectPlans)){
            projectPlans.forEach(item -> {
                item.setPlanId(nursingPlan.getId());
            });
            nursingProjectPlanMapper.insertBatch(projectPlans);
        }
    }

    /**
     * 根据id删除
     * @param id 计划id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        int count = nursingProjectMapper.findByPlanId(id);
        if(count> 0){
            throw new RuntimeException();
        }
        nursingPlanMapper.deleteById(id);
        nursingProjectPlanMapper.deleteByPlanId(id);
    }


    /**
     * 启动/禁用
     * @param id     计划id
     * @param status 计划状态 0 禁用 1 启用
     */
    @Override
    public void updateStatus(Long id, Long status) {
       nursingPlanMapper.updateStatus(id,status);
    }
}
